package com.example.demo.controller;

import com.example.demo.util.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/demo2")
public class Demo2Controller {
    @GetMapping("/test1")
    public String test1(HttpServletRequest request) {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest req = sra.getRequest();
        log.info("{}",req);
        log.info("{}",request);
        log.info("{}",req.getHeader("traceId"));
        log.info("{}",ThreadLocalUtil.traceId.get());
        return req.getRequestURI()+String.valueOf(req.equals(request));
    }
    @GetMapping("/test2")
    public String test2(HttpServletRequest request) {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest req = sra.getRequest();
        log.info("{}",req);
        log.info("{}",request);
        log.info("{}",req.getHeader("traceId"));
        log.info("{}",ThreadLocalUtil.traceId.get());
        return req.getRequestURI()+String.valueOf(req.equals(request));
    }
}
